package com.xzc.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Array;
import java.util.Arrays;

/**
 * @author 徐州城
 * @version 1.0
 */
public class ParameterAPIServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("用户名：" + req.getParameter("username"));
        System.out.println("密码：" + req.getParameter("password"));
        System.out.println("兴趣爱好：" + Arrays.asList(req.getParameterValues("hobby")));
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置请求的字符集为UTF-8，从而解决post请求中文乱码问题
        //req.setCharacterEncoding("UTF-8"); 在获取请求参数之前调用才有效 (要放在获取操作之前)
        req.setCharacterEncoding("UTF-8");
        System.out.println("用户名：" + req.getParameter("username"));
        System.out.println("密码：" + req.getParameter("password"));
        System.out.println("兴趣爱好：" + Arrays.asList(req.getParameterValues("hobby")));
    }
}
